package com.qf.fronted.servlet;

import com.qf.base.BaseServlet;
import com.qf.fronted.domain.City;
import com.qf.fronted.domain.District;
import com.qf.fronted.domain.Province;
import com.qf.fronted.service.selectaddress.ICityService;
import com.qf.fronted.service.selectaddress.IDistrictService;
import com.qf.fronted.service.selectaddress.IProvinceService;
import com.qf.fronted.service.selectaddress.impl.CityServiceImpl;
import com.qf.fronted.service.selectaddress.impl.DistrictServiceImpl;
import com.qf.fronted.service.selectaddress.impl.ProvinceServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/selectAddress")
public class SelectAddressServlet extends BaseServlet {
    private IProvinceService iProvinceService = new ProvinceServiceImpl();
    private ICityService iCityService = new CityServiceImpl();
    private IDistrictService iDistrictService = new DistrictServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String methodName = req.getParameter("method");

        try {
            Method method = SelectAddressServlet.class.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取省份
    private void getProvince(HttpServletRequest req, HttpServletResponse resp) {
        List<Province> provinces = iProvinceService.selectAllProvince();
        try {
            sendJson(resp, provinces);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取城市
    private void getCity(HttpServletRequest req, HttpServletResponse resp) {
        String provinceId = req.getParameter("provinceId");
        List<City> cities = iCityService.selectCityByProvinceId(Integer.parseInt(provinceId));
        try {
            sendJson(resp, cities);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取地区
    private void getDistrict(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        String cityId = req.getParameter("cityId");
        List<District> districtList = iDistrictService.getAllDistrictByCityId(Integer.parseInt(cityId));
        try {
            sendJson(resp, districtList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //通过城市名获取地区
    private void getDistrictByCityName(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        String cityName = req.getParameter("cityName");
        List<District> districtList = iDistrictService.getAllDistrictByCityName(cityName);
        try {
            sendJson(resp, districtList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
